package caches

import (
	"strconv"
	"time"

	"github.com/astaxie/beego/cache"
)

type Local struct {
	Cache cache.Cache
}

func (self *Local) SetLocal(HorseManId int, Longitude float64, Latitude float64) bool {
	id := strconv.Itoa(HorseManId)
	error := self.Cache.Put(id, map[string]interface{}{
		"Longitude": Longitude,
		"Latitude":  Latitude,
	}, time.Hour*24)
	return error == nil
}

func (self *Local) GetLocal(HorseManId int) (bool, interface{}) {
	id := strconv.Itoa(HorseManId)
	res := self.Cache.Get(id)
	return res == nil, res
}

func NewLocal() *Local {
	cache, error := cache.NewCache("redis", `{"conn":":6379","dbNum":"4"}`)
	if error != nil {
		panic("redis 链接失败")
	}
	return &Local{Cache: cache}
}
